/* eslint-disable react/react-in-jsx-scope */
import Taro,{ Component } from '@tarojs/taro'
import { View , Image , Text} from '@tarojs/components'

import BottomInfo from '../ItemBottomInfo/itemBottomInfo.js'
import phone_icon from '../../image/ic_phone.png'
import topTag from '../../image/ic_top_tag.png'

import {base_url} from "../../network/config";

import './jobItem.scss'
import DataManager from "../../utils/DataManager";


export default class jobItem extends Component {
  static options = {
    addGlobalClass: true
  }

  defaultProps = {
    jobData : {},
    isMine : false
  }

  jobItemClick = (jobData) =>{
    Taro.navigateTo({
      url: `/pages/job/jobDetail?jobId=` + jobData.id,
    })
  }

  makePhoneCall = (phoneNumber , e) =>{
    if (phoneNumber){
      Taro.makePhoneCall({
        phoneNumber : phoneNumber
      })
    }
    e.stopPropagation()  // 拦截事件冒泡
  }

  refreshJob = (jobData ,e) =>{
    e.stopPropagation()  // 拦截事件冒泡
    Taro.request({
      url:base_url + 'rest/pin/updateReleaseDate',
      data:{
        token:DataManager.getToken(),
        platform:'android_c',
        weixinId:DataManager.getWeiXinId(),
        loginChannel:'wechat',
        appId:'KC876D98B30UA40CJSNHI689FB33SDW',
        digest : 'dda484557c12febe48139d8f44051c13',
        id:jobData.id,
      },
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      method: 'POST',
      success(res2) {
        if (res2.data.status == 'success') {
          Taro.showToast({
            title:'刷新成功',
            icon:'none'
          })
          Taro.eventCenter.trigger('jobPost' ,[]);
        }
      }
    })

  }

  render() {
    let jobData = this.props.jobData;

    let salary = jobData.payScope ? jobData.payScope + '/月' : '';

    return (
      <View className='job-item-main-container' onClick={this.jobItemClick.bind(this,jobData)}>
        <View className='job-item-container'>
          <View className='job-item-container-sub'>
            <Image className='job-item-image' src={jobData.imageUrl} ></Image>
            <View className='job-item-sub'>
              <Text className='job-item-name'>{jobData.position}</Text>
              <Text className='job-item-salary'>{salary}</Text>
              <Text className='job-item-company'>{jobData.businessName}</Text>
            </View>
          </View>
          {!this.props.isMine ?
            <View className='job-item-phone' onClick={this.makePhoneCall.bind(this ,jobData.businessMobile)}>
              <Image className='job-item-phone-icon' src={phone_icon}></Image>
              <Text className='job-item-phone-text'>电话</Text>
            </View> :
            <View className='job-item-phone' onClick={this.refreshJob.bind(this ,jobData)}>
              <Text className='job-item-phone-text'>刷新</Text>
            </View>
          }
        </View>
        <View className='job-item-time-container'>
          <View className='job-item-time'>{jobData.releaseStrDate}</View>
          <View className='job-item-check'>查看详情 >></View>
        </View>
        <BottomInfo
          browseCount={jobData.viewsCount}
          praiseCount={jobData.praiseCount}
          shareCount={jobData.shareCount}
          itemId={jobData.id}
          itemType={1}
          hasPraise={jobData.isPraise}
        />
        <View className='job-item-divider'></View>
        {jobData.isTop && <Image className='job-top-icon' src={topTag}></Image>}
      </View>

    )
  }
}
